package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SalChance;
import cn.com.bdqn.service.SalChanceService;
import cn.com.bdqn.utils.ReturnData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/salChance")
@Api(tags = "销售机会管理接口")
public class SalChanceController {

    @Autowired
    private SalChanceService salChanceService;

    @Operation(summary = "销售机会高级查询")
    @PostMapping("/queryByLike")
    public ReturnData queryByLike(SalChance salChance, @RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "15") int pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<SalChance> salChances = salChanceService.queryByLike(salChance);
            PageInfo pageInfo = new PageInfo(salChances,3);
            return ReturnData.ok().put("salChances",salChances).put("pageInfo",pageInfo);
        }catch (Exception e){
            return ReturnData.currError("销售机会高级查询异常");
        }
    }

    @Operation(summary = "根据销售ID删除销售机会")
    @PostMapping("/delByChcId")
    public ReturnData delByChcId(Long chcId){
        int result = salChanceService.delByChcId(chcId);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("删除销售机会异常");
        }
    }

    @Operation(summary = "新增销售机会")
    @PostMapping("/addSalChance")
    public ReturnData addSalChance(SalChance salChance){
        int result = salChanceService.addSalChance(salChance);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("新增销售机会异常");
        }
    }

    @Operation(summary = "修改销售机会")
    @PostMapping("/updateSalChance")
    public ReturnData updateSalChance(SalChance salChance){
        int result = salChanceService.updateSalChance(salChance);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("修改销售机会异常");
        }
    }

    @Operation(summary = "指派销售机会")
    @PostMapping("/assign")
    public ReturnData assign(SalChance salChance){
        int result = salChanceService.assign(salChance);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("指派销售机会异常");
        }
    }

    @Operation(summary = "根据销售机会Id查询销售机会")
    @PostMapping("/queryById")
    public ReturnData queryByID(SalChance salChance){
        try {
            SalChance salChance1 = salChanceService.queryById(salChance);
            return ReturnData.ok().put("salChances",salChance1);
        }catch (Exception e){
            return ReturnData.currError("单个销售机会查询异常");
        }
    }

    @Operation(summary = "客户开发计划开发成功")
    @PostMapping("/devSuccess")
    public ReturnData devSuccess(Long chcId){
        int result = salChanceService.devSuccess(chcId);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.error();
        }
    }

    @Operation(summary = "客户开发计划开发失败")
    @PostMapping("/devFail")
    public ReturnData devFail(Long chcId){
        int result = salChanceService.devFail(chcId);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.error();
        }
    }

}
